package com.ruoyi.huanjing.service;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.DefaultXYZDataset;
import ucar.ma2.Array;
import ucar.ma2.Index;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;
import java.io.IOException;

public class Hot {

    public static void main(String[] args) {
        String filePath = "E:\\localfile\\MYD021KM.A2017314.0345.061.2017314164621.hdf";
        
        try (NetcdfFile ncFile = NetcdfFile.open(filePath)) {
            // 获取变量
            Variable thermalDetectorVar = ncFile.findVariable("Change_in_relative_responses_of_thermal_detectors");
            
            if (thermalDetectorVar == null) {
                System.err.println("变量 'Change_in_relative_responses_of_thermal_detectors' 未找到");
                return;
            }
            
            // 读取数据
            Array thermalData = thermalDetectorVar.read();
            
            // 获取数组维度
            int[] shape = thermalData.getShape();
            int bands = shape[0]; // number_of_emissive_bands
            int detectors = shape[1]; // detectors_per_1km_band
            
            System.out.println("数据维度: " + bands + " 波段 × " + detectors + " 探测器");
            
            // 创建Index对象用于访问数组元素
            Index index = thermalData.getIndex();
            
            // 转换为二维数组 (这里选择第一个波段的数据作为示例)
            short[][] detectorData = new short[1][detectors]; // 只显示第一个波段的数据
            
            for (int d = 0; d < detectors; d++) {
                detectorData[0][d] = thermalData.getShort(index.set(0, d));
            }
            
            // 绘制热力图
            plotHeatMap(detectorData, "热探测器相对响应变化");
            
        } catch (IOException e) {
            System.err.println("处理HDF文件时出错: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    private static void plotHeatMap(short[][] data, String title) {
        // 创建数据集
        DefaultXYZDataset dataset = new DefaultXYZDataset();
        
        // 准备数据
        int rows = data.length;
        int cols = data[0].length;
        double[] xValues = new double[rows * cols];
        double[] yValues = new double[rows * cols];
        double[] zValues = new double[rows * cols];
        
        int index = 0;
        for (int y = 0; y < rows; y++) {
            for (int x = 0; x < cols; x++) {
                xValues[index] = x;
                yValues[index] = y;
                zValues[index] = data[y][x];
                index++;
            }
        }
        
        dataset.addSeries("DetectorResponse", new double[][]{xValues, yValues, zValues});
        
        // 创建热力图
        JFreeChart chart = ChartFactory.createBubbleChart(
            title, "探测器位置", "波段", dataset, 
            PlotOrientation.VERTICAL, true, true, false);
        
        // 显示图表
        ChartFrame frame = new ChartFrame("热探测器响应热力图", chart);
        frame.pack();
        frame.setVisible(true);
    }
}